package project1;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * Stutter checks a text file for any repeated words.
 * It takes a filename as an argument and scans the words, character
 * by character, to report any repeated (i.e., stuttered) words.
 * 
 * @author Odessa
 */
public class Stutter 
{
	/** The previous word read from the file. */
	private static String previousWord = "";
	
	/** The word currently being read from the file. */
	private static String currentWord = "";
	
	/** True if the last character was a delimiter. */
	private static boolean isLastDelimiter = true;

	/**
	 * The main method.
	 * It accepts a file at run time and tries to open and 
	 * read the file. Then calls {@code determineStutter()}
	 * to determine if there are any repeated words.
	 *
	 * @param args the file to be read.
	 */
	public static void main(String[] args) 
	{
		String fileName = args[0];
		FileReader fileReader = null;
        BufferedReader bufferedReader = null;
		
        try {
        	fileReader = new FileReader(fileName);
        	bufferedReader = new BufferedReader(fileReader);
        	determineStutter(bufferedReader);
        }
        catch (IOException exception) {
        	System.out.println("Stutter couldn't open " + fileName);
        }
	}

	
	
	/**
	 * Reads the lines from the input stream.
	 * The characters in the input stream are then compared to the 
	 * delimiters[] array to determine if it is a delimiter, signaling
	 * the end of a word. When the end of a word is reached,
	 * {@code checkDuplicates()} is called to determine if it is the 
	 * same as the {@code previousWord}.
	 *
	 * @param bufferedReader the {@code BufferedReader}.
	 * @throws IOException If the file cannot be opened.
	 */
	private static void determineStutter(BufferedReader bufferedReader) 
		throws IOException 
	{
		int lineNo = 1;
		String thisLine = null;
		char c;
		
		while ((thisLine = bufferedReader.readLine()) != null)
		{
			for (int i = 0; i < thisLine.length(); i++)
			{
				c = thisLine.charAt(i);
				
				if (isDelimiter(c))
					checkDuplicate(lineNo);
				else {
					isLastDelimiter  = false;
					currentWord += c;
				}
			}
			
			checkDuplicate(lineNo);
			lineNo++;
		}
	}
	
	
	
	/**
	 * Checks to see if a character is a delimiter.
	 * It compares the parameter against the delimiters[] array. 
	 * If it is found in the array, then the character is a delimiter. 
	 *
	 * @param c the character to be compared.
	 * @return true, if the character is in the array and false otherwise.
	 */
	private static boolean isDelimiter(char c)
	{
		char[] delimiters = {'\n', '	', ' ', ',', '.', '!', '-', '+', 
							 '=', ';', ':', '?', '&', '{', '}', '\\'};
		
		for (int i = 0; i < delimiters.length; i++)
			if (c == delimiters[i])
				return true;
		
		return false;
	}
	
	
	
	
	/**
	 * Determines if there is a duplicate word.
	 * Checks if {@code currentWord} is the same as as the
	 * {@code previousWord} and prints a message if it is.
	 * 
	 * @param line the current line number. 
	 */
	private static void checkDuplicate(int line)
	{
		if (isLastDelimiter)
			return;
		
		isLastDelimiter = true;
		
		if (currentWord.equalsIgnoreCase(previousWord))
			System.out.println("Repeated word on line " + line + ": " +
								previousWord + " " + currentWord);
		else
			previousWord = currentWord;
		
		currentWord = "";
	}

}
